package com.zjitc.common.aspects;

import com.zjitc.model.test.TESTUser;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCommands;
import redis.clients.jedis.JedisPool;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/10 16:02
 * @description: 动态代理
 */
/**
 *暂时没用

* */


public class RedisProxy implements InvocationHandler{
  private JedisCommands target;
  private JedisPool pool;

  public RedisProxy(JedisPool pool1){
    pool = pool1 ;
  }

  public JedisCommands getInstance() {
    this.target = pool.getResource();
    Class clazz = target.getClass();
    return (JedisCommands) Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), this);
  }


  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
      return method.invoke(this.target, args);
    } finally {
      if (this.target instanceof Jedis) {
        ((Jedis) this.target).close();
        System.out.println("release the jedis connection");
      }
    }
  }

  public void close(Jedis jedis){
    jedis.close();
  }
}
